<template>
	<div class="page-wrap">
		<div class="page-content p-0">
			<div class="page-column p-0">
				<div class="page-form p-x-0">
					<el-form
						:model="ruleForm"
						:rules="rules"
						label-position="left"
						label-width="140px"
						ref="form"
						label-suffix="："
					>
						<el-row>
							<el-col :span="12">
								<el-form-item label="距离值设置" prop="regAmount">
									<el-input size="medium" v-model="ruleForm.regAmount">
										<template slot="append">km</template>
									</el-input>
									<p class="tips">多个距离输入请按照从小到大输入，以空隔分开；输入范围为[0.05,9999]</p>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div class="bottom-btn">
					<el-button type="primary" :loading="loading" @click="submitFun">下一步</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { getData } from "@/service/hd5.js"
import { mapState } from "vuex"
import { BATCH_SUBMIT } from "@/service/hd5.js"
export default {
	name: "",
	props: {
		batchid: String,
	},
	data() {
		var wxFee = (rule, value, callback) => {
			
			let tempdata = value.trim().split(/\s+/)
			let oldArr = JSON.parse(JSON.stringify(tempdata))
			let sortArr = tempdata.sort((a,b)=>{
				return a - b;
			})
			let onlySetSize = new Set(tempdata).size
			if (parseFloat(tempdata[tempdata.length - 1]).toString() == "NaN") {
				callback(new Error("请输入正确的格式"))
			} else if (
				tempdata[tempdata.length - 1] < 0.05 ||
				tempdata[tempdata.length - 1] > 9999
			) {
				callback(new Error("输入范围为[0.05,9999]"))
			} else if (
				(oldArr.join('-')!=sortArr.join('-'))
			) {
				callback(
					new Error("多个距离输入请按照从小到大输入，以空隔分开")
				)
			} else if(onlySetSize != tempdata.length) {
				callback(new Error("距离不可以重复"))
			}else{
				callback()
			}
		}
		return {
			activeTab: "2",
			ruleForm: {
				regAmount: "",
			},
			rules: {
				regAmount: [
					{
						required: true,
						message: "名称必填",
						trigger: ["change", "blur"],
					},
					{
						validator: wxFee,
						trigger: "change",
					},
				],
			},
			loading: false,
		}
	},
	computed: {
		...mapState({
			basic_Data: (state) => state.token.basic_Data,
		}),
	},
	methods: {
		setData() {
			this.ruleForm.regAmount = ""
			//将数组转为带有空格的字符串
			
			this.basic_Data.GESPAEND_DISTANCE.forEach((element) => {
				this.ruleForm.regAmount =
					this.ruleForm.regAmount + element + "\xa0"
			})
			
		},
		submitFun() {
			//保存
			this.$refs.form.validate((valid) => {
				if (valid) {
					let tempbasicData = JSON.parse(
						JSON.stringify(this.basic_Data)
					)
				
					let tempGESPAEND_DISTANCE = this.ruleForm.regAmount
						.trim()
						.split(/\s+/)
						.map((item) => {
							return parseFloat(item)
						})
					tempbasicData.GESPAEND_DISTANCE = tempGESPAEND_DISTANCE

					tempbasicData.GENUMRAD001 = tempGESPAEND_DISTANCE.length
					tempbasicData.SETPAGEINDEX = this.activeTab

					this.$store.dispatch("upData_basic_Data", tempbasicData)
					let params = {
						batchid: this.$props.batchid,
						batchinfo: "BASE",
						submitjson: JSON.stringify(tempbasicData),
					}
					this.$appApi
						.BATCH_SUBMIT(params)
						.then((res) => {
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "保存失败！",
									message: message,
									type: "success",
								})
							} else if (str == "S") {
								this.$notify({
									message: "保存成功！",
									type: "success",
								})
								this.$emit("changeActiveTab", this.activeTab)
					            this.$store.dispatch("change_basic_showTab", this.activeTab)
								if(activeTab >=9){
									this.$router.push({
										path: "/home",
									})
								}
							}
						})
						.catch((err) => {
							console.log(err)
						})

					
				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.page-flex-row /deep/ .el-collapse-item__header {
	background: #f3f9fe;
}
</style>